package com.utils.wx;


import com.bean.wx.ResultInfo;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

//处理返回的消息
@Component
public class WxReplyMessage {

    public String replyMessage(ResultInfo resultInfo, String type){
        String result="";
        switch (type){
            case "text":
                result=String.format(TextMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),
                        resultInfo.getContent());
                break;
            case "news":
                result=String.format(NewsMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),

                        resultInfo.getTitle(),
                        resultInfo.getDescription(),

                        resultInfo.getPicUrl(),
                        resultInfo.getUrl());
                break;
            case "image":
                result=String.format(ImageMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),
                        resultInfo.getMediaId());
                break;
            case "voice":
                result=String.format(VoiceMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),
                        resultInfo.getMediaId());
                break;
            case "video":
                result=String.format(VideoMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),
                        resultInfo.getMediaId(),

                        resultInfo.getTitle(),
                        resultInfo.getDescription());
                break;
            case "music":
                result=String.format(MusicMessage,
                        resultInfo.getToUserName(),
                        resultInfo.getFromUserName(),
                        resultInfo.getCreteTime(),

                        resultInfo.getTitle(),
                        resultInfo.getDescription(),

                        resultInfo.getMusicUrl(),
                        resultInfo.getHqMusicUrl(),
                        resultInfo.getThumbMediaId());
                break;
        }
        return result;
    }


    //回复文本消息
    private final String TextMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>%s</CreateTime>\n" +
            "  <MsgType><![CDATA[text]]></MsgType>\n" +
            "  <Content><![CDATA[%s]]></Content>\n" +
            "</xml>";
    //回复图文消息
    private final String NewsMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>%s</CreateTime>\n" +
            "  <MsgType><![CDATA[news]]></MsgType>\n" +
            "  <ArticleCount>1</ArticleCount>\n" +
            "  <Articles>\n" +
            "    <item>\n" +
            "      <Title><![CDATA[%s]]></Title>\n" +
            "      <Description><![CDATA[%s]]></Description>\n" +
            "      <PicUrl><![CDATA[%s]]></PicUrl>\n" +
            "      <Url><![CDATA[%s]]></Url>\n" +
            "    </item>\n" +
            "  </Articles>\n" +
            "</xml>\n";
    //回复图片消息
    private final String ImageMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>12345678</%s>\n" +
            "  <MsgType><![CDATA[image]]></MsgType>\n" +
            "  <Image>\n" +
            "    <MediaId><![CDATA[%s]]></MediaId>\n" +
            "  </Image>\n" +
            "</xml>";
    //回复语音消息
    private final String VoiceMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>%s</CreateTime>\n" +
            "  <MsgType><![CDATA[voice]]></MsgType>\n" +
            "  <Voice>\n" +
            "    <MediaId><![CDATA[%s]]></MediaId>\n" +
            "  </Voice>\n" +
            "</xml>";
    //回复视频消息
    private final String VideoMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>%s</CreateTime>\n" +
            "  <MsgType><![CDATA[video]]></MsgType>\n" +
            "  <Video>\n" +
            "    <MediaId><![CDATA[%s]]></MediaId>\n" +
            "    <Title><![CDATA[%s]]></Title>\n" +
            "    <Description><![CDATA[%s]]></Description>\n" +
            "  </Video>\n" +
            "</xml>";
    //回复音乐消息
    private final String MusicMessage=
            "<xml>\n" +
            "  <ToUserName><![CDATA[%s]]></ToUserName>\n" +
            "  <FromUserName><![CDATA[%s]]></FromUserName>\n" +
            "  <CreateTime>%s</CreateTime>\n" +
            "  <MsgType><![CDATA[music]]></MsgType>\n" +
            "  <Music>\n" +
            "    <Title><![CDATA[%s]]></Title>\n" +
            "    <Description><![CDATA[%s]]></Description>\n" +
            "    <MusicUrl><![CDATA[%s]]></MusicUrl>\n" +
            "    <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>\n" +
            "    <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>\n" +
            "  </Music>\n" +
            "</xml>";


    public  String getUtcTime() {
        Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间
        DateFormat df = new SimpleDateFormat("yyyyMMddhhmm");// 设置显示格式
        String nowTime = df.format(dt);
        long dd = (long) 0;
        try {
            dd = df.parse(nowTime).getTime();
        } catch (Exception e) {

        }
        return String.valueOf(dd);
    }
}
